﻿using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;


namespace IDE_Eyes
{
    public partial class EyesForm : Form
    {
        private string serverName;
        private int mouseX, mouseY;
        private bool open = false;
        private bool firstTimeFormLoaded = true;
        private int formHeight; //ques.
        //private string serverName; //ques. 10
      

        public EyesForm()
        {
            InitializeComponent();
            //ques. 9
            formHeight = ClientSize.Height;
        }
       

        private void eyesPanel_Paint(object sender, PaintEventArgs e)
        {
            drawEyes(mouseX, mouseY, eyesPanel.Width, eyesPanel.Height, e.Graphics);

            if (paintCheckBox.CheckState == CheckState.Checked)
                writeMessage("eyesPanel: Paint");

            //quest. 3
            // close the eyes when open is false
            // background color of the form must turn gray
           if (open == false)
            {
                closeEyes(eyesPanel.Width, eyesPanel.Height, e.Graphics);
            }
            else
            {
                open = true;
            }
           

        }

        private void eyesPanel_MouseMove(object sender, MouseEventArgs e)
        {

            if (mouseMoveCheckBox.CheckState==CheckState.Checked)
                writeMessage("eyesPanel: (" + e.X + "," + e.Y + ") MouseMove: (" + e.X + "," + e.Y + ")");

            //quest. 1
            mouseX = e.X;
            mouseY = e.Y;

            Refresh();
            
        }

        private void EyesForm_Activated(object sender, EventArgs e)
        {
            //quest. 4
            if (firstTimeFormLoaded == false)
                this.BackColor = System.Drawing.Color.White;

            firstTimeFormLoaded = false;

            open = !open;
            Refresh();
            writeMessage("EyesForm: Activated");
        }

        private void EyesForm_Deactivate(object sender, EventArgs e)
        {
            //quest. 3
            this.BackColor = System.Drawing.Color.Gray;
            open = !open;
            Refresh();
            writeMessage("EyesForm: Deactivate");
        }

        private void Form1_Shown(object sender, EventArgs e)
        {

            writeMessage("EyesForm: Shown");
        }

        private void EyesForm_MouseMove(object sender, MouseEventArgs e)
        {

            if (mouseMoveCheckBox.CheckState == CheckState.Checked)
                writeMessage("EyesForm: MouseMove: (" + e.X + "," + e.Y + ")");

            //
            mouseX = e.X;
            mouseY = e.Y;
            Refresh();

            /*while (message!=WM_QUIT)
            {  
               "Process message";   
               "Get next message from Message Queue";
            }*/

        }

        private void EyesForm1_Load(object sender, EventArgs e)
        {
            //At startup, the background of the form must be black. 
            //ques. 5
            writeMessage("EyesForm: Load");
            this.BackColor = System.Drawing.Color.Black;

            //The form starting transparent, and gradually becoming opaque. 
            //ques. 6
            	for (double opacity = 0.0; opacity <= 1.0; opacity += 0.01)
                {
                    DateTime start = DateTime.Now;
                    this.Opacity = opacity;
                    while (DateTime.Now.Subtract(start).TotalMilliseconds <= 10.0)
                        Application.DoEvents();
                }
        }

        private void EyesForm_ResizeBegin(object sender, EventArgs e)
        {   //ques. 8 and 9
            if (formHeight != ClientSize.Height)
            {
                BackColor = Color.White;
            }
            else
            {
                BackColor = Color.Red;
            }
            writeMessage("EyesForm: ResizeBegin");
        }

        private void EyesForm_ResizeEnd(object sender, EventArgs e)
        {   //ques. 8 and 9
            formHeight = ClientSize.Height;
            BackColor = Color.White;

            writeMessage("EyesForm: ResizeEnd");
        }

        private void EyesForm_FormClosed(object sender, FormClosedEventArgs e)
        {

            writeMessage("EyesForm: FormClosed");
        }

        private void EyesForm_FormClosing(object sender, FormClosingEventArgs e)
        {//10

            writeMessage("EyesForm: FormClosing");


             //if (serverName.Length == 0)
            //{

                // Initializes the variables to pass to the MessageBox.Show method. 

                string message = "Close this operation?";
                string caption = "Error Detected in Input";
                MessageBoxButtons buttons = MessageBoxButtons.OKCancel;
                DialogResult result;

                //Make e.Cancel true when the result of MessageBox.Show is DialogResult.Cancel, and false otherwise 

                // Displays the MessageBox.

                result = MessageBox.Show(message, caption, buttons);


                if (result == System.Windows.Forms.DialogResult.Cancel)
                {

                    e.Cancel = true;

                    // Closes the parent form. 

                    this.Dispose();

                }
                else
                {
                    e.Cancel = false;
                   // this.Close();

                }

            //}

        }

        private void EyesForm_Move(object sender, EventArgs e)
        {

            
                //ques. 7 and 9
            if (formHeight != ClientSize.Height)
            {
                BackColor = Color.Red;
            }
            else
            {
                BackColor = Color.White;
            }
           
            writeMessage("EyesForm: Move");
        }

        /// <summary>
        /// With this method, the eyes are drawn in a closed state
        /// </summary>
        /// <param name="width"></param> The total width of the area in which the eyes are drawn
        /// <param name="height"></param> The total height of the area in which the eyes are drawn
        /// <param name="eyeArea"></param> The Graphics object used to draw the eyes
        private void closeEyes(int width, int height, Graphics eyeArea)
        {
            int xleft = 0, yleft = 0, xright = 0, yright = 0, reye = 0, rpupil = 0;
            xleft = width / 3;
            yleft = height / 2;
            xright = 2 * width / 3;
            yright = height / 2;
            reye = width / 9;
            rpupil = reye / 2;
            eyeArea.FillEllipse(Brushes.Gray, xleft - reye, yleft - reye, 2 * reye, 2 * reye);
            eyeArea.FillEllipse(Brushes.Gray, xright - reye, yright - reye, 2 * reye, 2 * reye);

        }

        /// <summary>
        /// With this method, the eyes are drawn, and look at a certain point
        /// </summary>
        /// <param name="lookAtX"></param> The x coordinate of the point the eyes are looking at
        /// <param name="lookAtY"></param> The y coordinate of the point the eyes are looking at
        /// <param name="width"></param> The total width of the area in which the eyes are drawn
        /// <param name="height"></param> The total height of the area in which the eyes are drawn
        /// <param name="eyeArea"></param> The Graphics object used to draw the eyes
        private void drawEyes(int lookAtX, int lookAtY, int width, int height, Graphics eyeArea)
        {
            int xleft = 0, yleft = 0, xright = 0, yright = 0, xpleft = 0, ypleft = 0, xpright = 0, ypright = 0, reye = 0, rpupil = 0;
            xleft = width / 3;
            yleft = height / 2;
            xright = 2 * width / 3;
            yright = height / 2;
            reye = width / 9;
            rpupil = reye / 2;
            Bitmap bufl = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bufl);

            g.FillEllipse(Brushes.White, xleft - reye, yleft - reye, 2 * reye, 2 * reye);
            g.FillEllipse(Brushes.White, xright - reye, yright - reye, 2 * reye, 2 * reye);
            g.DrawEllipse(Pens.Black, xleft - reye, yleft - reye, 2 * reye, 2 * reye);
            g.DrawEllipse(Pens.Black, xright - reye, yright - reye, 2 * reye, 2 * reye);
            if ((lookAtX != xleft) || (lookAtY != yleft))
            {
                xpleft = (int)Math.Round(xleft + (reye - rpupil) / (Math.Sqrt(Math.Pow(lookAtX - xleft, 2) + Math.Pow(lookAtY - yleft, 2))) * (lookAtX - xleft));
                ypleft = (int)Math.Round(yleft + (reye - rpupil) / (Math.Sqrt(Math.Pow(lookAtX - xleft, 2) + Math.Pow(lookAtY - yleft, 2))) * (lookAtY - yleft));
            }
            else
            {
                xpleft = xleft;
                ypleft = yleft;
            }
            if ((lookAtX != xright) || (lookAtY != yright))
            {
                xpright = (int)Math.Round(xright + (reye - rpupil) / (Math.Sqrt(Math.Pow(lookAtX - xright, 2) + Math.Pow(lookAtY - yright, 2))) * (lookAtX - xright));
                ypright = (int)Math.Round(yright + (reye - rpupil) / (Math.Sqrt(Math.Pow(lookAtX - xright, 2) + Math.Pow(lookAtY - yright, 2))) * (lookAtY - yright));
            }
            else
            {
                xpright = xright;
                ypright = yright;
            }
            g.FillEllipse(Brushes.Black, xpleft - rpupil, ypleft - rpupil, 2 * rpupil, 2 * rpupil);
            g.FillEllipse(Brushes.Black, xpright - rpupil, ypright - rpupil, 2 * rpupil, 2 * rpupil);
            eyeArea.DrawImage(bufl, 0, 0);
            g.Dispose();
        }

        private void writeMessage(string message)
        {
            messagesListBox.Items.Add(message);
            messagesListBox.TopIndex = messagesListBox.Items.Count - 1;
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            messagesListBox.Items.Clear();
        }

        private void messagesListBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void EyesForm_MouseEnter(object sender, EventArgs e)
        {
            //ques. 7
            BackColor = Color.White;

        }
        //ques. 10
        //private virtual Int32 serverName { get; }
       /* private void validateUserEntry(object sender, FormClosingEventArgs e)
        {

            // Checks the value of the text. 
            //public virtual Int32 serverName {get;}
            //private string serverName;     


        }*/

    }
}
